package com.swust.excel.write.fill;

import com.alibaba.excel.EasyExcel;
import com.swust.excel.entity.ExportRiverAffairsDTO;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;


public class FillMain {
    public static void main(String[] args) {
        // 方案1 根据对象填充
        String fileName = "d://test.xlsx";
        String templateFileName = FillMain.class.getResource("/").getPath() + "fill/river_affairs.xlsx";
        /*
        * 如果是网页接口，读取模板得换一种形式，如下所示
                RiverAffairsOrder dto = riverAffairsOrders.get(0);
                //返回体设置
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding("utf-8");
                String excelName = dto.getCreateTimeYm() + "_" + dto.getUnitName() + ".xlsx";
                excelName = URLEncoder.encode(excelName, "UTF-8");
                response.setHeader("Content-disposition", "attachment;filename=" + excelName);
                //
                ClassPathResource classPathResource = new ClassPathResource(File.separator + "templates" + File.separator + "river_affairs.xlsx");
                EasyExcel.write(response.getOutputStream(), RiverAffairsOrder.class)
                        .withTemplate(classPathResource.getInputStream())
                        .autoCloseStream(Boolean.FALSE)
                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                        .sheet()
                        .doFill(dto);
        * */

        // 这里 会填充到第一个sheet， 然后文件流会自动关闭
//        FillData fillData1 = new FillData();
//        fillData1.setName("张三");
//        fillData1.setSex("男");
//
//        EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(fillData1);

        ExportRiverAffairsDTO dto = new ExportRiverAffairsDTO();
        dto.setAfforestNow(111);

        EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(dto);
    }

    @Data
    public static class FillData{
        private String name;
        private String sex;
    }
}
